<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page contentType="text/html;charset=utf-8"%> 
<%@ page import="java.io.*" %>
<%@ page import ="org.jdom.input.SAXBuilder" %>
<%@ page import ="org.jdom.*"%>
<%@ page import ="org.jdom.output.*"%>
<%@ page import="net.sf.jasperreports.engine.*,net.sf.jasperreports.engine.util.JRProperties" %>

<%@ page import="net.sf.jasperreports.engine.util.*" %>
<%@ page import="net.sf.jasperreports.engine.export.*" %>
<%@ page import="net.sf.jasperreports.j2ee.servlets.*" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="java.sql.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'sheet1_p_cpl.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

	</head>

	<body>
		<%
		
		try
        {
			SAXBuilder sb = new SAXBuilder();
			String thepath = application.getRealPath("/reports/sheet1_p_cpl.jrxml");
	        Document doc = sb.build(thepath);
	        Element root = doc.getRootElement();
	        List list = root.getChildren();
	
	        Element queryString = (Element)list.get(0);
	       	System.out.println(queryString.getValue().toString());
	       	queryString.removeContent();
	       	
	      	queryString.addContent(new CDATA("select * from jreport1 where apartmentname='二号楼'")); 
	        System.out.println(queryString.getValue().toString());
	        
	        //生成临时文件
	        Format format = Format.getCompactFormat();  
	        format.setEncoding("utf-8"); //设置xml文件的字符为utf-8
	        format.setIndent("    "); //设置xml文件的缩进为4个空格  
	        XMLOutputter XMLOut = new XMLOutputter(format);//元素后换行一层元素缩四格   
	        XMLOut.output(doc, new FileOutputStream(application.getRealPath("/reports/sheet1_p_cpl_tmp.jrxml")));   
       		
       		
       		//编译jrxml文件
	       JasperCompileManager.compileReportToFile(application.getRealPath("/reports/sheet1_p_cpl_tmp.jrxml"),application.getRealPath("/reports/sheet1_p_cpl_tmp.jasper"));
	        
           
        }
        catch (Exception e) {
           	e.printStackTrace();
        }

		//以HTML输出
	
	String strUrl = "jdbc:mysql://localhost:3306/dms";
	Connection conn = null;
	try {
			conn = DriverManager.getConnection(strUrl, "root", "udbvj");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	File reportFile = new File(application.getRealPath("/reports/boshi_xueyuan.jasper"));
    if (!reportFile.exists())
		throw new JRRuntimeException("File boshi_xueyuan.jasper not found. The report design must be compiled first.");

	JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());

	Map parameters = new HashMap();
	parameters.put("ReportTitle", "Address Report");
	parameters.put("BaseDir", reportFile.getParentFile());
				
	JasperPrint jasperPrint = 
		JasperFillManager.fillReport(
			jasperReport, 
			parameters, 
			conn
			);
				
	JRHtmlExporter exporter = new JRHtmlExporter();

	session.setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
	
	exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
	exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out);
	exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "../servlets/image?image=");
	
	exporter.exportReport();
	conn.close();
	
	
	/*
	File file = new File(application.getRealPath("/reports/sheet1_p_cpl_tmp.jrxml"));
	file.delete();
	file = new File(application.getRealPath("/reports/sheet1_p_cpl_tmp.jasper"));
	file.delete();
   */
    %>
	</body>
</html>
